package com.lovemanai.taglib;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * 带标签体标签---迭代器标签
 * @author lovemanai
 *
 */
public class IteratorTag extends SimpleTagSupport{
	
	//标签属性，指定被迭代的集合
	private String collection;
	//标签属性，指定迭代集合元素，为集合元素指定名称
	private String item;
	
	public String getCollection() {
		return collection;
	}
	public void setCollection(String collection) {
		this.collection = collection;
	}
	public String getItem() {
		return item;
	}
	public void setItem(String item) {
		this.item = item;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void doTag() throws JspException, IOException {
		// 从page scop中获取名为collection的集合
		Collection<Object> itemList = (Collection<Object>)getJspContext().getAttribute(collection);
		//循环遍历
		for (Object b : itemList) {
			//集合元素在page范围内
			getJspContext().setAttribute(item, b);
			//输出到标签体
			getJspBody().invoke(null);
		}
	}
	
	
}
